﻿using Blog.Service;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Blog.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CrudController<T> : ControllerBase
    {
        private readonly ICrudService<T> _curdService;

        public CrudController(ICrudService<T> crudService)
        {
            _curdService = crudService;
        }
        // GET: api/<CurdController>
        [HttpGet]
        public async Task<IEnumerable<T>> Get()
        {
            return await _curdService.GetAll();
        }

        // GET api/<CurdController>/5
        [HttpGet("{id}")]
        public async Task<T> Get(dynamic id)
        {
            return await _curdService.Find(id);
        }

        // POST api/<CurdController>
        [HttpPost]
        public async Task<bool> Post(T t)
        {
             return await _curdService.Add(t);
        }

        // PUT api/<CurdController>/5
        [HttpPut]
        public async Task<bool> Put( T value)
        {
            return await _curdService.Update(value);
        }

        // DELETE api/<CurdController>/5
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _curdService.SoftDelete(id);
        }
    }
}
